package com.example.vue20220225.controller;

import com.example.vue20220225.service.UserService;
import com.example.vue20220225.util.ImageUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/person")
public class PersonController {

    @Resource
    UserService userService;

    // 导出用户列表
    @RequestMapping("/importExcel")
    public void importExcel( HttpServletResponse response){
        userService.importExcel(response);
    }



    @RequestMapping("/fileUpload")
    public Map<String , Object> fileUpload(MultipartFile file,String userid){
        Map<String , Object> map = new HashMap<>();
        //判断文件是否为空
        if(file.isEmpty()){
            map.put("message","文件为空");
            return map;
        }
        //文件名
        String fileName = file.getOriginalFilename();
        System.out.println(fileName);
        // 判断是否为图片
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        System.out.println(suffixName);
        //JPG、JPEG、PNG、GIF
        if(!".png".equalsIgnoreCase(suffixName) && !".gif".equalsIgnoreCase(suffixName) &&
         !".jpg".equalsIgnoreCase(suffixName) &&  !".jpeg".equalsIgnoreCase(suffixName)){
            map.put("message","文件格式错误");
            return map;
        }
        //  避免图片和项目一起打包  图片存放地址和项目分离
        String absolutePath = "D:\\Users\\image";
        File dest = new File(absolutePath+"\\"+fileName);
        System.out.println(dest.getAbsolutePath());

        String filename = "http://localhost:8080/file/"+
                fileName.substring(0,fileName.lastIndexOf("."))+
                "-thumbnail"+suffixName;
        // 文件移植
        try {
            if(!dest.exists()){
                file.transferTo(dest);
                String[] files = new String[]{dest.getAbsolutePath()};
                List<String> strings = ImageUtil.generateThumbnail2Directory(absolutePath, files);
                for(String str : strings){
                    System.out.println(str+"11");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("filename",filename);
        //  用户表 加字段  通过userid  修改 头像的路径
        System.out.println(userid);
        //  调用mapper 修改
        //  服务代码
        return map;
    }

}
    /**
     * 测试在指定目录下生成缩略图
     */
//    @Test
//    public void testGenerateThumbnail2Directory() throws IOException {
//        String path = "D:\\workspace\\idea\\individual\\springboot-learn\\springboot-thumbnail\\image";
//        String[] files = new String[]{
//                "image/1.jpg",
//                "image/2.jpg"
//        };
//
//        List<String> list = ImageUtil.generateThumbnail2Directory(path, files);
//        System.out.println(list);
//    }
//
//    /**
//     * 将指定目录下的图片生成缩略图
//     */
//    @Test
//    public void testGenerateDirectoryThumbnail() throws IOException {
//        String path = "D:\\workspace\\idea\\individual\\springboot-learn\\springboot-thumbnail\\image";
//        ImageUtil.generateDirectoryThumbnail(path);
//    }